const {_getBaseServer,_deleteBaseServer,updateBaseServer, _gettotalBaseServer, addBaseServer, SelectBaseServer} = require('../service/Base')


module.exports = {
    // 获取数据的方法
    async _getBase(ctx){
        const {page,size}  = ctx.query;
        const result = await _getBaseServer(page,size)
        let total = await _gettotalBaseServer()
        if(result.length){
            ctx.body = {
                code:1,
                message:'获取成功',
                result,
                total:total.length
            }
        }else{
            ctx.body = {
                code:0,
                message:'获取失败'
            }
        }
    },
    async _deleteBase(ctx){
        console.log(ctx.query);
        const {id} = ctx.query
        let result = await _deleteBaseServer(id)
        console.log(result);
        if(result.affectedRows==1){
            ctx.body = {
                code:1,
                message:'删除成功'
            }
        }else{
            ctx.body = {
                code:0,
                message:'删除失败'
            }
        }
    },
    // 添加
    async _addBase(ctx){
        // console.log(ctx.request.body);
        const {username,email,time,address,remark,img} = ctx.request.body
        // console.log(img);
        let result = await addBaseServer({username,email,time,address,remark,img})
        // console.log(result);
        if(result.affectedRows==1){
            ctx.body = {code:1,message:'添加成功',result}
        }else{
            ctx.body = {code:0,message:'添加失败'}
        }
    },
    // 修改
    async _updataBase(ctx){
        console.log(ctx.request.body);
        let {id,username,email,time,address,remark,img} = ctx.request.body
        let result = await updateBaseServer({id,username,email,time,address,remark})
        if(result.affectedRows==1){
            ctx.body = {code:1,message:'修改成功'}
        }else{
            ctx.body = {code:0,message:'修改失败'}
        }
    },
    async SelectBase(ctx){
        let {username} = ctx.query
        console.log(ctx.query);
        // console.log(username,email,time,address);
        let result = await SelectBaseServer({username})
        console.log(result);
        if(result.length){
            ctx.body={
                code:1,
                message:'获取成功',
                result:result
            }
        }else{
            ctx.body={
                code:0,
                message:'获取失败'
            }
        }
    }
}